package projectwork.shchuangge.cn.com.newproject.ui.activity;

import android.Manifest;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import projectwork.shchuangge.cn.com.newproject.R;
import projectwork.shchuangge.cn.com.newproject.clearedittext.ClearEditText;
import projectwork.shchuangge.cn.com.newproject.clearedittext.PasswordEditText;
import projectwork.shchuangge.cn.com.newproject.projectwork.BasicActivity;
import projectwork.shchuangge.cn.com.newproject.ui.view.MyVideoView;
import projectwork.shchuangge.cn.com.newproject.uitls.PasswordNameSave;
import pub.devrel.easypermissions.EasyPermissions;

import static projectwork.shchuangge.cn.com.newproject.projectwork.Chuangge.context;


/**
 * Created by 赵航 on 2017/6/15.
 */

public class LoginActivity extends BasicActivity implements View.OnClickListener {

    private MyVideoView      mVideoView;
    private ImageView        mCanso;
    private Button           mLogin;
    private PasswordEditText mPasswordEditText;
    private ClearEditText    mLoginEditText;
    private TextView         mRegistText;
    private TextView         mForgotNumber;
    private TextView         mFastLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //初始化所有的控件
        initView();

        //添加数据
        initData();
        System.out.println("two");
    }

    private void initData() {

        //循环播放视频
        playViode();

        //进行图片点击监听
        mCanso.setOnClickListener(this);

        //登录进行监听
        mLogin.setOnClickListener(this);

        //跳转到注册界面
        mRegistText.setOnClickListener(this);
        //忘记密码
        mForgotNumber.setOnClickListener(this);
        //手机快速登录
        mFastLogin.setOnClickListener(this);
    }

    private void playViode() {
        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.welcome_video));
        //播放
        mVideoView.start();
        //循环播放
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {

                mVideoView.start();
            }
        });
    }


    private void initView() {

        mVideoView = (MyVideoView) findViewById(R.id.login_videview);

        //取消登录按钮
        mCanso = (ImageView) findViewById(R.id.hsk_sole_title_left_image);

        //登录按钮
        mLogin = (Button) findViewById(R.id.button_login);

        //初始化账号跟密码输入框
        mPasswordEditText = (PasswordEditText) findViewById(R.id.edt_password);
        mLoginEditText = (ClearEditText) findViewById(R.id.edt_login);
        //注册
        mRegistText = (TextView) findViewById(R.id.text_register);
        //忘记密码
        mForgotNumber = (TextView) findViewById(R.id.text_fog_password);
        //手机快速登录
        mFastLogin = (TextView) findViewById(R.id.fast_login_text);
        //动态监听输入框的数字个数

        mPasswordEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {
                System.out.println(s+"之前的"+start+"  ,"+after);

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,int count) {
                System.out.println(s+"之后的"+start+"  ,"+count);
                String mima = s.toString();


            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


      //TODO:获取手机设备型号ID进行验证见面的跳转
//        TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//         String DEVICE_ID = tm.getDeviceId();
        getSigle();
    }

    private void getSigle() {

        //获取手机唯一串号
        String szImei = null;
        if (EasyPermissions.hasPermissions(LoginActivity.this, Manifest.permission.READ_PHONE_STATE)) {
            TelephonyManager TelephonyMgr = (TelephonyManager) LoginActivity.this
                    .getSystemService(Context.TELEPHONY_SERVICE);
            szImei = TelephonyMgr.getDeviceId();

        } else {
            // EasyPermissions.requestPermissions(context, “请您为打开唯一标志权限”,
            // 2, Manifest.permission.READ_PHONE_STATE);

        }

//
//        String m_szDevIDShort = "35" + //we make this look like a valid IMEI
//                Build.BOARD.length() % 10 +
//                Build.BRAND.length() % 10 +
//                Build.CPU_ABI.length() % 10 +
//                Build.DEVICE.length() % 10 +
//                Build.DISPLAY.length() % 10 +
//                Build.HOST.length() % 10 +
//                Build.ID.length() % 10 +
//                Build.MANUFACTURER.length() % 10 +
//                Build.MODEL.length() % 10 +
//                Build.PRODUCT.length() % 10 +
//                Build.TAGS.length() % 10 +
//                Build.TYPE.length() % 10 +
//                Build.USER.length() % 10; //13 digits
//
//        String m_szAndroidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
//
//
//        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//        String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
//
//
//        String m_szLongID = szImei + m_szDevIDShort
//                + m_szAndroidID + m_szWLANMAC;
//        // compute md5
//        MessageDigest m = null;
//        try {
//            m = MessageDigest.getInstance("MD5");
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }
//        m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
//        // get md5 bytes
//        byte p_md5Data[] = m.digest();
//        // create a hex string
//        String m_szUniqueID = new String();
//        for (int i = 0; i < p_md5Data.length; i++) {
//            int b = (0xFF & p_md5Data[i]);
//            // if it is a single digit, make sure it have 0 in front (proper padding)
//            if (b <= 0xF)
//                m_szUniqueID += "0";
//            // add number to string
//            m_szUniqueID += Integer.toHexString(b);
//        }   // hex string to uppercase
//        m_szUniqueID = m_szUniqueID.toUpperCase();
//
//        Log.e("手机唯一标志deviceId", m_szUniqueID);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.hsk_sole_title_left_image:
                myStartActivity(MainActivity.class,true);
                finish();
                break;
            case R.id.button_login:
                //获取输入的字符

                String pwd = mPasswordEditText.getText().toString().trim();
                String name = mLoginEditText.getText().toString().trim();
                //加密保存字账户
                PasswordNameSave.savePassword(context,pwd,name,true);
                //加密获账户
                String[] strings = PasswordNameSave.readPassword(context);
                String pwd1 = strings[0];
                String name1 = strings[1];
                System.out.println(pwd1);
                System.out.println(name1);
                //TODO:根据逻辑,这里相当于对话框,只是登陆后就消失
                myStartActivity(MainActivity.class,true);
                break;
            case R.id.text_register:
                myStartActivity(RegisterActivity.class,false);
                break;
            case R.id.text_fog_password:
                myStartActivity(FrogotMnuber.class,false);
                break;
            case R.id.fast_login_text:
                myStartActivity(FastLongin.class,false);
                break;
        }
    }

    /**
     * 直接返回主界面
     */
//    public void onBackPressed() {
//        Intent MyIntent = new Intent(Intent.ACTION_MAIN);
//        MyIntent.addCategory(Intent.CATEGORY_HOME);
//        startActivity(MyIntent);
//    }

    /**
     * 当界面不可见时
     */
    @Override
    protected void onPause() {
        //添加代码的健壮性,防止内存泄漏
        if (mVideoView!=null&&mVideoView.isPlaying()) {
            mVideoView.stopPlayback();
        }
        super.onPause();
    }

    /**
     * 当界面可见时
     */
    @Override
    protected void onResume() {
        //必须先判空
        if (mVideoView!=null&&mVideoView.isPlaying()) {
            //循环播放视频
            playViode();;
        }


        //防止锁屏或者切出的时候，音乐在播放

        initData();
        super.onResume();
    }
}
